/*
 * 
 */
package com.flying.dcss.dcauthorization.action;

import java.util.LinkedList;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.flying.dcss.app.SessionName;
import com.flying.dcss.dcauthorization.bo.impl.DcssApplicationOrder;
import com.flying.dcss.dcauthorization.service.impl.AuthorizeService;
import com.flying.dcss.dcmanager.bo.impl.DcssDCInfo;
import com.flying.dcss.dcmanager.bo.impl.DcssDCTreeInfo;
import com.flying.dcss.dcmanager.service.impl.DCInfoService;
import com.flying.dcss.dcmanager.service.impl.DCTreeService;
import com.flying.dcss.userpermissions.bo.impl.DcssUser;
import com.flying.dcss.userpermissions.service.impl.UserInfoService;
import com.flying.dcss.userpermissions.service.impl.UserService;
import com.flying.dcss.viewmodel.impl.OrderListViewModel;
import com.opensymphony.xwork2.ActionSupport;

// TODO: Auto-generated Javadoc
/**
 * The Class SelectFormListByDcIdAction.
 */
public class SelectFormListByDcIdAction extends ActionSupport {

	/** The authorize service. */
	private AuthorizeService authorizeService;

	/** The user info service. */
	private UserInfoService userInfoService;

	/** The user service. */
	private UserService userService;

	/** The dc info service. */
	private DCInfoService dcInfoService;

	/** The dc tree service. */
	private DCTreeService dcTreeService;

	/**
	 * Sets the authorize service.
	 * 
	 * @param authorizeService
	 *            the new authorize service
	 */
	public void setAuthorizeService(AuthorizeService authorizeService) {
		this.authorizeService = authorizeService;
	}

	/**
	 * Sets the user info service.
	 * 
	 * @param userInfoService
	 *            the new user info service
	 */
	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}

	/**
	 * Sets the user service.
	 * 
	 * @param userService
	 *            the new user service
	 */
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	/**
	 * Sets the dc info service.
	 * 
	 * @param dcInfoService
	 *            the new dc info service
	 */
	public void setdcInfoService(DCInfoService dcInfoService) {
		this.dcInfoService = dcInfoService;
	}

	/**
	 * Sets the dc tree service.
	 * 
	 * @param dcTreeService
	 *            the new dc tree service
	 */
	public void setdcTreeService(DCTreeService dcTreeService) {
		this.dcTreeService = dcTreeService;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub

		// Type=0 : Get all.
		// Type=1 : Get Pending.
		// Type=2 : Get UnPassed.
		// Type=3 : Get Passed.
		int type = Integer.parseInt(ServletActionContext.getRequest()
				.getParameter("orderType"));
		String dcId = ServletActionContext.getRequest().getParameter("dcId");

		if (dcId == null || dcId.length() == 0) {
			DcssUser user = (DcssUser) ServletActionContext.getRequest().getSession()
					.getAttribute(SessionName.UserSession);
			// Get all application order.
			List<DcssApplicationOrder> orders = authorizeService
					.getApplicationOrderByApproverId(user.getId(), type);

			if (orders == null) {
				return null;
			}

			// Complete view model.
			List<OrderListViewModel> models = new LinkedList<OrderListViewModel>();
			for (DcssApplicationOrder item : orders) {

				DcssDCInfo dcInfo = dcInfoService.getById(item.getDcId());

				DcssDCTreeInfo floor = dcTreeService.getById(dcInfo
						.getFatherId());

				DcssDCTreeInfo building = dcTreeService.getById(floor
						.getFatherId());

				DcssDCTreeInfo city = dcTreeService.getById(building
						.getFatherId());

				OrderListViewModel model = new OrderListViewModel(
						item,
						userService.getUserById(item.getUserId()),
						userInfoService.getUserInfoByUserId(item.getUserId()),
						userService.getUserById(item.getApprover()),
						userInfoService.getUserInfoByUserId(item.getApprover()),
						dcInfo, city.getDescription(), building
								.getDescription(), floor.getDescription());
				models.add(model);
			}

			// Write session.
			ServletActionContext.getRequest().setAttribute(
					SessionName.OrderListSession, models);
			return this.SUCCESS;
		}
		// Get all application order.
		List<DcssApplicationOrder> orders = authorizeService
				.getApplicationOrderByDcId(dcId, type);

		if (orders == null) {
			return null;
		}

		// Complete view model.
		List<OrderListViewModel> models = new LinkedList<OrderListViewModel>();
		for (DcssApplicationOrder item : orders) {

			DcssDCInfo dcInfo = dcInfoService.getById(item.getDcId());

			DcssDCTreeInfo floor = dcTreeService.getById(dcInfo.getFatherId());

			DcssDCTreeInfo building = dcTreeService
					.getById(floor.getFatherId());

			DcssDCTreeInfo city = dcTreeService.getById(building.getFatherId());

			OrderListViewModel model = new OrderListViewModel(item, userService
					.getUserById(item.getUserId()), userInfoService
					.getUserInfoByUserId(item.getUserId()), userService
					.getUserById(item.getApprover()), userInfoService
					.getUserInfoByUserId(item.getApprover()), dcInfo, city
					.getDescription(), building.getDescription(), floor
					.getDescription());
			models.add(model);
		}

		// Write session.
		ServletActionContext.getRequest().setAttribute(
				SessionName.OrderListSession, models);

		return this.SUCCESS;
	}
}
